/**
 * 游戏好友排行
 */
class FriendRank extends UIBase{
    private btn_close:BaseButton;
    private tab_1:eui.Group;
    private tab_2:eui.Group;
    private img_di1:eui.Image;
    private img_di2:eui.Image;
    private curSelectIndex:number;

    private rank_group1:eui.Group;
    private btn_next:BaseButton;
    private btn_last:BaseButton;
    private curPage:number = 0;

    private img_tittle_1:eui.Image;
    private img_tittle_2:eui.Image;

    private list = [];
    constructor()
    {
        super('FriendRankView',{showBG:true});
    }

    public init()
    {
        super.init(true);
        this.curSelectIndex = 1;
        this.requestData();
        this.getRank();
        UIManager.instance().hideLamp();
    }

    protected changeUI(data)
    {
        let index = data[0];
        if(index == this.curSelectIndex)return;
        if(this.curSelectIndex == 1)
        {
            this.curSelectIndex = 2;
        }
        else
        {
            this.curSelectIndex = 1;
        }
        let bool = this.curSelectIndex == 1;
        this.img_di1.visible = bool;
        this.img_di2.visible = !bool;
        this.img_tittle_1.source = bool ? 'new_rank_5_png':'new_rank_5_1_png';
        this.img_tittle_2.source = !bool ? 'new_rank_6_png':'new_rank_6_1_png';
        this.requestData();
    }

    public requestData()
    {
        let type = this.curSelectIndex == 1 ? 'score' : 'points'
        RankManager.instance().requestFriendCloudStorage(type);
    }

    public getRank()
    {
        let bitmap_rank = WeChatgGameManager.instance().createRankListDisplayObject();
        if(bitmap_rank)
        {
            bitmap_rank.name = 'bitmap_rank';
            this.rank_group1.addChild(bitmap_rank);
        }
    }

    public addEvent()
    {
        this.btn_close.addTapHandler(this.close, this);
        this.tab_1.addEventListener(egret.TouchEvent.TOUCH_TAP, this.changeUI.bind(this, [1]), this);
        this.tab_2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.changeUI.bind(this, [2]), this);

        this.btn_last.addTapHandler(this.LeftBtnOnClick, this);
        this.btn_next.addTapHandler(this.RightBtnOnClick, this);
    }
     
    protected LeftBtnOnClick()
    {
        if(this.curPage <= 0)
        {
            MessageUtil.tips("到了最前面了哦");
            return;
        }
        this.curPage --;
        RankManager.instance().requestLastPageData();
    }

    protected RightBtnOnClick()
    {
        this.curPage ++;
        RankManager.instance().requestNextPageData();
    }

    protected close()
    {
        this.remove(1);
    }

    public removeEvent()
    {
        this.btn_close.removeTapHandler();
        
        this.tab_1.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.changeUI, this);
        this.tab_2.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.changeUI, this);
    }

    public clear()
    {
        super.clear();
        RankManager.instance().closeFriendCloudStorage();
        this.curPage = null;
        this.curSelectIndex = null;
        this.list = null;
        UIManager.instance().showLamp();
    }
}